package com.ifonly.activerecord.providers;

import com.alibaba.druid.pool.DruidDataSource;
import com.ifonly.config.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-18 16:23
 * @since JDK 1.6
 */
public class DruidDataSourceProvider implements DataSourceProvider {
    private static final String DB_INITIALSIZE = "db.initialSize";
    private static final String DB_MINIDLE = "db.minIdle";
    private static final String DB_MAXACTIVE = "db.maxActive";

    private static final String DB_MAXWAIT = "db.maxWait";
    private static final String DB_TIMEBETWEENEVICTIONRUNSMILLIS = "db.timeBetweenEvictionRunsMillis";
    private static final String DB_MINEVICTABLEIDLETIMEMILLIS = "db.minEvictableIdleTimeMillis";

    private static Configuration configuration = Configuration.getInstance();

    private String username;
    private String password;
    private String url;

    public DruidDataSourceProvider() {
        this(configuration.getJdbcUsername(),
                configuration.getJdbcPassword(),
                configuration.getJdbcUrl());
    }

    public DruidDataSourceProvider(String username, String password, String url) {
        this.username = username;
        this.password = password;
        this.url = url;
    }

    @Override
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);

        try {
            /*String filters = "wall";
            if (configuration.isDruidStat()) {
                filters = filters + ",stat";
            }
            dataSource.setFilters(filters);*/
            dataSource.setFilters("wall,stat");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        int initialSize = configuration.getInt(DB_INITIALSIZE, -1);
        if (initialSize != -1) {
            dataSource.setInitialSize(initialSize);
        }

        int minIdle = configuration.getInt(DB_MINIDLE, -1);
        if (minIdle != -1) {
            dataSource.setMinIdle(minIdle);
        }

        int maxActive = configuration.getInt(DB_MAXACTIVE, -1);
        if (maxActive != -1) {
            dataSource.setMaxActive(maxActive);
        }

        long maxWait = configuration.getLong(DB_MAXWAIT, -1L);
        if (maxWait != -1) {
            dataSource.setMaxWait(maxWait);
        }

        long timeBetweenEvictionRunsMillis = configuration.getLong(DB_TIMEBETWEENEVICTIONRUNSMILLIS, -1L);
        if (timeBetweenEvictionRunsMillis != -1) {
            dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }

        long minEvictableIdleTimeMillis = configuration.getLong(DB_MINEVICTABLEIDLETIMEMILLIS, -1);
        if (minEvictableIdleTimeMillis != -1) {
            dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }

        return dataSource;
    }

}
